﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_CS
{
    public partial class phuluc3_update1 : MyFromBase
    {
        private static string SelectedID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            if (!IsPostBack)
            {
                BindDataToDropDownList(ddlKyBaoCao, "KyBaoCao");
                ddlKyBaoCao.Items.Insert(0, new ListItem("Chọn kỳ báo cáo", "-1"));
                lblDonVi.Text = MyCommon.GetTenDonVi(Session["userid"].ToString());
                BindDataToGrid();
            }

        }
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("PhuLuc3");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {

                Response.Redirect("Index.aspx?p=0");

            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataToGrid();

        }

        protected void btnThem_Click(object sender, EventArgs e)
        {

        }

        protected void grvData_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvData.PageIndex = e.NewPageIndex;
            BindDataToGrid();
        }
        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SelectedID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;
            if ((sCommandName == "Detail"))
            {
                string sql = "Select KyBaoCaoID_DM,BaoCaoSo From tblPhuLuc3 Where id='" + SelectedID + "' ";
                SqlDataReader myReader = SqlHelper.ExecuteReader(CommandType.Text, sql);
                myReader.Read();
                MyReportNumber = myReader["BaoCaoSo"].ToString();
                MyReportPeriod = myReader["KyBaoCaoID_DM"].ToString();
                MySwitchCase = e.CommandName;
                MyReportID = SelectedID;
                MyReportFullName = "Phụ lục 3";
                Response.Redirect("PhuLuc_Edit.aspx");
            }
            if ((sCommandName == "Sua"))
            {
                string sql = "Select KyBaoCaoID_DM,BaoCaoSo From tblPhuLuc3 Where id='" + SelectedID + "' ";
                SqlDataReader myReader = SqlHelper.ExecuteReader(CommandType.Text, sql);
                myReader.Read();
                MyReportNumber = myReader["BaoCaoSo"].ToString();
                MyReportPeriod = myReader["KyBaoCaoID_DM"].ToString();
                MySwitchCase = e.CommandName;
                MyReportID = SelectedID;
                MyReportFullName = "Phụ lục 3";
                Response.Redirect("PhuLuc_Edit.aspx");
            }
            if (sCommandName == MyConst.Action.Delete)
            {
                string sql = "DELETE FROM dbo.tblPhuLuc3 WHERE ID = @ID";
                List<SqlParameter> arParams = new List<SqlParameter>();
                arParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, SelectedID));
                int result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arParams);
                if (result > 0)
                {
                    MyCommon.AddMessage(this.Master, "Bạn đã xóa thành công 1 bản ghi", MyConst.MessageType.Success);
                    BindDataToGrid();
                }
            }
        }
        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RowDataBound(sender, e);
        }
        protected void ddlKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        protected void BindDataToDropDownList(DropDownList BindDropDown, string MaDM)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDM", SqlDbType.NVarChar, ParameterDirection.Input, MaDM));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM=@MaDM", arrParams);
            BindDropDown.DataSource = dt;
            BindDropDown.DataTextField = "TenDM";
            BindDropDown.DataValueField = "ID";
            BindDropDown.DataBind();
        }
        private void BindDataToGrid()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            if (ddlKyBaoCao.SelectedItem.Text.Trim() == "Chọn kỳ báo cáo")
                arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "%" + "" + "%"));
            else
                arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "%" + ddlKyBaoCao.SelectedValue.ToString().Trim() + "%"));
            arrParams.Add(SqlHelper.CreateParameterObject("@SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "%" + txtReportNumber.Text.Trim() + "%"));
            arrParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, Session["DonVi_ID"]));



            string sql = "select a.id,a.DonVi_ID,ROW_NUMBER() over (order by (select 1)) as SoThuTu,a.BaoCaoSo as BaoCaoSo,dv.TenDV, b.TenDM as KyBaoCao,  CONVERT(VARCHAR(10),a.createdate,103) as Created, CONVERT(VARCHAR(10),a.updatedate,103) as Updated from tblPhuLuc3 a, tblDanhMuc b, tblDonVi dv" +
            " where a.KyBaoCaoID_DM=b.id And a.DonVi_ID = dv.ID And KyBaoCaoID_DM Like @KyBaoCao And BaoCaoSo Like @SoBaoCao And a.DonVi_ID In (Select ID From tblDonVi Where ParentID Like @parentID+'%' Or ParentID Like +'%;'+@parentID+'%' )";


            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;

                DataRow dr = dt.NewRow();
                dr[0] = 0;
                dt.Rows.InsertAt(dr, 0);

            }

            grvData.DataSource = dt;
            grvData.DataBind();
            if (IsEmpty)
            {
                grvData.Rows[0].Visible = false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            MyReportNumber = txtReportNumber.Text.Trim();
            MyReportPeriod = ddlKyBaoCao.SelectedValue;
            MySwitchCase = "Null";
            MyReportFullName = "Phụ lục 3";
            Response.Redirect("PhuLuc_Edit.aspx");

        }
    }
}
